package com.sam.platmform.unimplements;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class PlatformBRHelper {
    private static PlatformBRHelper self;
    public synchronized static PlatformBRHelper getInstance(){
        if(self == null) self = new PlatformBRHelper();
        return self;
    }

    //==============================================================================================
    //                         进入蓝牙板块的广播
    //==============================================================================================
    private static final String ENTER_BLE_BROADCAST_RECEIVER = "COM.SAM.ENTER.BLE.ACTION";
    public  void registerEnterBLEBR(Context context){
        EnterBLEBR receiver = new EnterBLEBR();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ENTER_BLE_BROADCAST_RECEIVER);
        context.registerReceiver(receiver, filter);
    }
    public void sendEnterBLEBR(Context context){
        Intent intent = new Intent(ENTER_BLE_BROADCAST_RECEIVER);
        context.sendBroadcast(intent);
    }

    private EnterBLECallback enterBLECallback;
    public void setEnterBLECallback(EnterBLECallback enterBLECallback){
        this.enterBLECallback = enterBLECallback;
    }

    class EnterBLEBR extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(ENTER_BLE_BROADCAST_RECEIVER)){
                if(enterBLECallback != null)enterBLECallback.enterBLEModule();
            }
        }
    }

    public interface EnterBLECallback{
        void enterBLEModule();
    }
    //==============================================================================================
    //                         登录成功的广播
    //==============================================================================================
    private static final String LOGIN_SUCCEEDED_BROADCAST_RECEIVER = "COM.SAM.LOGIN.SUCCEEDED.ACTION";
    public  void registerLoginSecceededBR(Context context){
        LoginSecceededBR receiver = new LoginSecceededBR();
        IntentFilter filter = new IntentFilter();
        filter.addAction(LOGIN_SUCCEEDED_BROADCAST_RECEIVER);
        context.registerReceiver(receiver, filter);
    }
    public void sendLoginSecceededBR(Context context,String account,String passwd,String token,String id){
        Intent intent = new Intent(LOGIN_SUCCEEDED_BROADCAST_RECEIVER);
        intent.putExtra("account",account);
        intent.putExtra("passwd",passwd);
        intent.putExtra("token",token);
        intent.putExtra("id",id);
        context.sendBroadcast(intent);
    }

    private LoginSecceededCallback loginSecceededCallback;
    public void setLoginSecceededCallback(LoginSecceededCallback loginSecceededCallback){
        this.loginSecceededCallback = loginSecceededCallback;
    }

    class LoginSecceededBR extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(LOGIN_SUCCEEDED_BROADCAST_RECEIVER)){
                if(loginSecceededCallback != null){
                    String account = intent.getStringExtra("account");
                    String passwd = intent.getStringExtra("passwd");
                    String token = intent.getStringExtra("token");
                    String id = intent.getStringExtra("id");
                    loginSecceededCallback.onLoginSucceeded(account,passwd,token,id);
                }
            }
        }
    }

    public interface LoginSecceededCallback{
        void onLoginSucceeded(String account,String passwd,String token,String id);
    }
    //==============================================================================================
    //                         进入设置页面的广播
    //==============================================================================================
    private static final String GOTO_SETTINGS_BROADCAST_RECEIVER = "COM.SAM.SETTINGS.ACTION";
    public  void registerGotoSettingsAction(Context context){
        LoginSecceededBR receiver = new LoginSecceededBR();
        IntentFilter filter = new IntentFilter();
        filter.addAction(GOTO_SETTINGS_BROADCAST_RECEIVER);
        context.registerReceiver(receiver, filter);
    }
    public void sendGotoSettingsBR(Context context,String account,String passwd,String token,String id){
        Intent intent = new Intent(GOTO_SETTINGS_BROADCAST_RECEIVER);
        context.sendBroadcast(intent);
    }

    private GotoSettingsCallback gotoSettingsCallback;
    public void setGotoSettingsCallback(GotoSettingsCallback gotoSettingsCallback){
        this.gotoSettingsCallback = gotoSettingsCallback;
    }

    class GotoSettinsBR extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(GOTO_SETTINGS_BROADCAST_RECEIVER)){
                if(gotoSettingsCallback != null) gotoSettingsCallback.gotoSettingsPage();
            }
        }
    }

    public interface GotoSettingsCallback{
        void gotoSettingsPage();
    }
}
